package com.cs.auth.application.controller;

import com.cs.auth.application.dtos.AuthUserDTO;
import com.cs.auth.common.bos.AuthCaptchaBO;
import com.cs.auth.common.bos.AuthUserBO;
import com.cs.auth.common.entity.BaseResponse;
import com.cs.auth.common.utlis.ResultUtils;
import com.cs.auth.domain.service.AuthUserDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/auth/user")
public class AuthUserController {

    @Resource
    private AuthUserDomainService authUserDomainService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public BaseResponse<Boolean> register(@RequestBody AuthUserDTO authUserDTO){
        // 输出日志
        log.info("用户注册请求参数：{}", authUserDTO);
        // 类型转换
        AuthUserBO authUserBO = new AuthUserBO();
        BeanUtils.copyProperties(authUserDTO, authUserBO);
        AuthCaptchaBO authCaptchaBO = new AuthCaptchaBO();
        BeanUtils.copyProperties(authUserDTO.getAuthCaptchaDTO(), authCaptchaBO);
        authUserBO.setAuthCaptchaBO(authCaptchaBO);
        // 调用domain层方法
        return ResultUtils.success(authUserDomainService.register(authUserBO));
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public BaseResponse<AuthUserDTO> login(@RequestBody AuthUserDTO authUserDTO){
        // 输出日志
        log.info("用户登录请求参数：{}", authUserDTO);
        // 类型转换
        AuthUserBO authUserBO = new AuthUserBO();
        BeanUtils.copyProperties(authUserDTO, authUserBO);
        AuthCaptchaBO authCaptchaBO = new AuthCaptchaBO();
        BeanUtils.copyProperties(authUserDTO.getAuthCaptchaDTO(), authCaptchaBO);
        authUserBO.setAuthCaptchaBO(authCaptchaBO);
        // 调用domain层方法
        AuthUserBO loginUser = authUserDomainService.login(authUserBO);
        //类型转换
        AuthUserDTO loginUserDTO = new AuthUserDTO();
        BeanUtils.copyProperties(loginUser, loginUserDTO);
        return ResultUtils.success(loginUserDTO);
    }
}
